package com.atmc.utils;

import org.springframework.stereotype.Component;

import java.io.*;

/**
 * @BelongsProject: SpringCloud1216
 * @BelongsPackage: com.com.atmc.utils
 * @Author: chuyings
 * @CreateTime: 2022-11-01  14:22
 * @Version: 1.0
 */
@Component
public class FileTransformUtils {
    public static void fileTrans(String firstPath,String endPath){
        File file1=new File(firstPath);
        File file2=new File(endPath);

        BufferedInputStream bis= null;
        BufferedOutputStream bos= null;
        try {
            FileInputStream fis=new FileInputStream(file1);
            FileOutputStream fos=new FileOutputStream(file2);
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            byte[] b=new byte[1024];
            int len;
            while((len=bis.read(b)) !=-1 ){
                bos.write(b,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(bos !=null){
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(bis !=null){
                    bis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void fileInTrans(InputStream is,String endPath)   {
        File file=new File(endPath);

        BufferedInputStream bis1= null;
        BufferedOutputStream bos1= null;
        try {
//            FileInputStream fis=new FileInputStream(file3);
            FileInputStream fis1= (FileInputStream) is;
            FileOutputStream fos1=new FileOutputStream(file);
            bis1 = new BufferedInputStream(fis1);
            bos1 = new BufferedOutputStream(fos1);
            byte[] b=new byte[1024];
            int len;
            while((len=bis1.read(b)) !=-1 ){
                bos1.write(b,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(bos1 !=null){
                    bos1.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(bis1 !=null){
                    bis1.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
